package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.Strategy;
import cn.wolfcode.wolf2w.business.api.domain.StrategyRank;
import cn.wolfcode.wolf2w.business.mapper.StrategyMapper;
import cn.wolfcode.wolf2w.business.mapper.StrategyRankMapper;
import cn.wolfcode.wolf2w.business.query.StrategyRankQuery;
import cn.wolfcode.wolf2w.business.service.IStrategyRankService;
import cn.wolfcode.wolf2w.business.vo.DestVO;
import cn.wolfcode.wolf2w.business.vo.RanksVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 攻略排行Service业务层处理
 *
 * @author 沙琪玛
 * @date 2024-11-01
 */
@Service
@Transactional
public class StrategyRankServiceImpl extends ServiceImpl<StrategyRankMapper, StrategyRank> implements IStrategyRankService {
    @Autowired
    private StrategyMapper strategyMapper;

    @Override
    public IPage<StrategyRank> queryPage(StrategyRankQuery qo) {
        IPage<StrategyRank> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public List<StrategyRank> queryRank(Integer type) {
        return lambdaQuery().eq(StrategyRank::getType, type)
                .inSql(StrategyRank::getStatisTime, "select MAX(statis_time) from ta_strategy_rank")
                .list();
    }

    @Override
    public void createRank() {
        LambdaQueryWrapper<Strategy> qw = new LambdaQueryWrapper<>();
        qw.eq(Strategy::getIsabroad, 1)
                .orderByDesc(Strategy::getViewnum)
                .last("limit 10");
        List<Strategy> abroadList = strategyMapper.selectList(qw);

        qw.clear();
        qw.eq(Strategy::getIsabroad, 0)
                .orderByDesc(Strategy::getViewnum)
                .last("limit 10");
        List<Strategy> chinaList = strategyMapper.selectList(qw);

        qw.clear();
        qw.orderByDesc(Strategy::getViewnum)
                .last("limit 10");
        List<Strategy> hotList = strategyMapper.selectList(qw);

        Date now = new Date();

        List<StrategyRank> rankList = new ArrayList<>();

        for (Strategy strategy : abroadList) {
            StrategyRank rank = new StrategyRank();
            rank.setStrategyId(strategy.getId());
            rank.setStrategyTitle(strategy.getTitle());
            rank.setDestName(strategy.getDestName());
            rank.setDestId(strategy.getDestId());
            rank.setStatisnum(strategy.getViewnum());

            rank.setType(1L);
            rank.setStatisTime(now);
            rankList.add(rank);
        }
        for (Strategy strategy : chinaList) {
            StrategyRank rank = new StrategyRank();
            rank.setStrategyId(strategy.getId());
            rank.setStrategyTitle(strategy.getTitle());
            rank.setDestName(strategy.getDestName());
            rank.setDestId(strategy.getDestId());
            rank.setStatisnum(strategy.getViewnum());

            rank.setType(2L);
            rank.setStatisTime(now);
            rankList.add(rank);
        }
        for (Strategy strategy : hotList) {
            StrategyRank rank = new StrategyRank();
            rank.setStrategyId(strategy.getId());
            rank.setStrategyTitle(strategy.getTitle());
            rank.setDestName(strategy.getDestName());
            rank.setDestId(strategy.getDestId());
            rank.setStatisnum(strategy.getViewnum());

            rank.setType(3L);
            rank.setStatisTime(now);
            rankList.add(rank);
        }

        this.saveBatch(rankList);
        System.err.println("排行榜统计完成////");
    }

    @Override//旅游攻略主题推荐
    public List<RanksVO> queryTheme() {
//        LambdaQueryWrapper<Strategy> qw = new LambdaQueryWrapper<>();
//        qw.groupBy(Strategy::getThemeId);
        QueryWrapper<Strategy> qw = new QueryWrapper<>();
        qw.groupBy("theme_id").select("theme_id,theme_name,count(0) count").orderByDesc("count").last("limit 10");
        List<RanksVO> list = new ArrayList<>();
        List<Strategy> strategies = strategyMapper.selectList(qw);
        for (Strategy strategy : strategies) {
            RanksVO ranksVO = new RanksVO();
            String themeName = strategy.getThemeName();
            ranksVO.setThemeName(themeName);
            LambdaQueryWrapper<Strategy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Strategy::getThemeName,themeName);
            List<Strategy> strategyList = strategyMapper.selectList(lambdaQueryWrapper);
            ArrayList<DestVO> list1 = new ArrayList<>();
            for (Strategy s : strategyList) {
                DestVO destVO = new DestVO();
                destVO.setId(s.getDestId());
                destVO.setName(s.getDestName());
                list1.add(destVO);
            }
            ranksVO.setDests(list1);
            list.add(ranksVO);

        }
        System.err.println(list);
        return list;
//        // 查询所有策略，并按主题ID分组
//        LambdaQueryWrapper<Strategy> qw = new LambdaQueryWrapper<>();
//        List<Strategy> allStrategies = strategyMapper.selectList(qw);
//
//        // 使用Map存储每个主题下的策略列表
//        Map<String, List<Strategy>> themeToStrategyMap = allStrategies.stream()
//                .collect(Collectors.groupingBy(Strategy::getThemeName));
//
//        // 构建最终的结果列表
//        List<RanksVO> resultList = themeToStrategyMap.entrySet().stream()
//                .map(entry -> {
//                    RanksVO ranksVO = new RanksVO();
//                    ranksVO.setThemeName(entry.getKey());
//
//                    // 将策略转换为DestVO对象
//                    List<DestVO> dests = entry.getValue().stream()
//                            .map(strategy -> {
//                                DestVO destVO = new DestVO();
//                                destVO.setId(strategy.getDestId());
//                                destVO.setName(strategy.getDestName());
//                                return destVO;
//                            })
//                            .collect(Collectors.toList());
//
//                    ranksVO.setDests(dests);
//                    return ranksVO;
//                })
//                .collect(Collectors.toList());
//
//        System.err.println(resultList);
//        return resultList;
    }
}
